package com.mallcloud.mall.app.task.xgl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mallcloud.mall.app.service.AppOrderCouponService;
import com.mallcloud.mall.common.core.constant.enums.mall.OrderEnum;
import com.mallcloud.mall.mall.api.entity.MallOrderCouponModel;
import com.mallcloud.mall.mall.service.MallOrderCouponService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 星光里【券订单】定时处理回调成功，却处理失败的订单
 */
@Slf4j
@RequiredArgsConstructor
//@Configuration      //1.主要用于标记配置类，兼备Component的效果。
//@ConditionalOnProperty("const.wxpay.v3.appId")
public class CouponOrderNotifyJob {
    private final MallOrderCouponService orderCouponService;
    private final AppOrderCouponService appOrderCouponService;

    @SneakyThrows
    @Scheduled(cron = "0 30 23 * * ? ")
    private void xglPayTasks() {
        log.info("=========");
        log.info("=========");
        log.info("=========");
        log.info("=========");
        log.info("券订单补偿....");
        List<MallOrderCouponModel> orderList = orderCouponService.list(new LambdaQueryWrapper<MallOrderCouponModel>()
                .eq(MallOrderCouponModel::getStatus, OrderEnum.StatusEnum.FAIL.getStatus())
                .eq(MallOrderCouponModel::getHasPayed, true)
                .ge(MallOrderCouponModel::getCreateTime, DateUtil.beginOfDay(new Date()))
        );
        log.info("补偿券订单 {}", orderList.stream().map(MallOrderCouponModel::getOrderNo).collect(Collectors.toList()));
        orderList.forEach(OrderCoupon -> {
            log.info("补偿券订单 {}", OrderCoupon.getOrderNo());
			appOrderCouponService.handleNotifyByWxPay(OrderCoupon);
        });
        log.info("券订单补偿结束");
    }
}